<?php
/**
 * Created by PhpStorm.
 * User: sqq
 * Date: 2018/09/07
 * Time: 18:32
 */

use Illuminate\Support\Facades\Redis;
class ApiArticleController extends BaseController {

    /**
     * @Author:      sqq
     * @DateTime:    2018-09-11 09:18:28
     * @Description: C端3.0 首页文章模块
     * @param  $token  String  用户token
     * @link: http://www.activity.com/wechat/api3.0/article/homePageArticle?source=1
     */
    public function homePageArticle(){
        $validator = Validator::make(Input::all(),
            array(
                'source' =>'required',
            )
        );
        do{
            if($validator->fails()) {
                $response = Lang::get("ahp_error.args_error_1000");
                $response["result"]=$validator->messages()->all();
                break;
            }

            //获取参数
            $source = Input::get('source');

            //查询文章分类
            $articleList = ArticleTypeWechat3::where('status',1)->select('id','type_name')->get()->toArray();
            if(!is_null($articleList)){
                foreach($articleList as $_t => $_p){
                    $articleList[$_t]['article'] = ArticleWechat3::where('source','LIKE','%'.$source.'%')->where('article_type',$_p['id'])->where('is_recommend',2)->where('status',1)->orderBy('updated_at','desc')->select('id','first_image','is_recommend','title')->take(6)->get()->toArray();
                }
            }

            //推荐
            $is_recommend = ArticleWechat3::where('source','LIKE','%'.$source.'%')->where('is_recommend',1)->where('status',1)->orderBy('updated_at','desc')->select('id','first_image','is_recommend','title')->take(6)->get()->toArray();
            if($is_recommend){
                $list_recommend = Config::get('article_share_wechat3.list_recommend_icon');
                $list_recommend['article'] = $is_recommend;
                array_unshift($articleList, $list_recommend);
            }
            if($articleList){
                foreach($articleList as $key => $val){
                    if(isset($val['article']) && $val['article']){
                        foreach($val['article'] as $_k => $_v){
                            $articleList[$key]['article'][$_k]['article_first_image_file_url'] = Config::get('article_share_wechat3.article_first_image_file_url');
                        }
                    }else{
                        unset($articleList[$key]);
                    }
                }
            }

            $response = array(
                'code' => 0,
                'msg' => 'ok',
                'result' => $articleList
            );
        }while(false);
        Log::info("info", array("system" => "api3.0" ,"event" => "homePageArticle", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-09-11 09:18:28
     * @Description: C端3.0 首页文章模块
     * @param  $token  String  用户token
     * @link: http://www.activity.com/wechat/api3.0/article/articleList?source=1&page_size=10&article_type=1
     */
    public function articleList(){
        $validator = Validator::make(Input::all(),
            array(
                'source' =>'required',
                'article_type' =>'required',
            )
        );
        do{
            if($validator->fails()) {
                $response = Lang::get("ahp_error.args_error_1000");
                $response["result"]=$validator->messages()->all();
                break;
            }

            //获取参数
            $source = Input::get('source');
            $article_type = Input::get('article_type');
            $page_size=Input::get("page_size",10);
            $page=Input::get("page",1);

            //查询文章分类
            $articleType = ArticleTypeWechat3::where('status',1)->select('id','type_name')->get()->toArray();
            $articleList = ArticleWechat3::join('article_type_wechat3','article_wechat3.article_type','=','article_type_wechat3.id')->where('source','LIKE','%'.$source.'%')->where('article_type',$article_type)->where('article_wechat3.status',1);
            $articleList = $articleList->leftjoin('article_keyword_relation_wechat3','article_wechat3.id','=','article_keyword_relation_wechat3.article_id');
            if(Input::get('keyword_id')){
                //$articleList = $articleList->join('article_keyword_relation_wechat3','article_wechat3.id','=','article_keyword_relation_wechat3.article_id');
                    $articleList = $articleList->where('article_keyword_relation_wechat3.keyword_id',Input::get('keyword_id'));
            }
            if(Input::get('title')){
                $key_id = ArticleKeywordWechat3::where('keyword_name', 'LIKE', '%' . Input::get('title') . '%')->select('id')->get()->toArray();
                if (!empty($key_id)) {
                    $key_id = array_column($key_id, 'id');
                }
                $articleList = $articleList->where('article_wechat3.title','LIKE','%'.Input::get('title').'%')->orWhereIn('article_keyword_relation_wechat3.keyword_id', $key_id);
            }
              $articleList = $articleList->orderBy('updated_at','desc')->distinct()->select('article_wechat3.id','list_image','article_wechat3.is_recommend','article_wechat3.title','article_wechat3.updated_at','article_type_wechat3.type_name','article_wechat3.keyword_id')->skip($page_size*($page-1))->take($page_size)->get()->toArray();

            if($articleList){
                foreach($articleList as $key => $val){
                    $keyword_id = explode(',',$val['keyword_id']);
                    $keyword = ArticleKeywordWechat3::whereIn('id',$keyword_id)->where('status',1)->select('id','keyword_name')->get();
                    $articleList[$key]['keyword'] = $keyword;
                    $articleList[$key]['article_list_image_file_url'] = Config::get('article_share_wechat3.article_list_image_file_url');
                }
            }

            $response = array(
                'code' => 0,
                'msg' => 'ok',
                'result' => [
                    'articleType' => $articleType,
                    'articleList' => $articleList
                ]
            );
        }while(false);
        Log::info("info", array("system" => "api3.0" ,"event" => "articleList", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-09-11 13:18:28
     * @Description: C端3.0 文章详情
     * @param  $token  String  用户token
     * @link: http://www.activity.com/wechat/api3.0/article/articleShow?source=1&id=23
     */
    public function articleShow(){
        $validator = Validator::make(Input::all(),
            array(
                'source' =>'required',
                'id' =>'required',
               // 'identification' =>'required',
            )
        );
        do{
            if($validator->fails()) {
                $response = Lang::get("ahp_error.args_error_1000");
                $response["result"]=$validator->messages()->all();
                break;
            }

            //获取参数
            $source = Input::get('source');
            $id = Input::get('id');
            $identification = Input::get('identification');
            $type = Input::get('type',1);  //是否是后端预览 1-否 2-是

            if($type == 2){
                $articleList = ArticleWechat3::join('article_type_wechat3','article_wechat3.article_type','=','article_type_wechat3.id')->where('article_wechat3.id',$id)->select('article_wechat3.id','list_image','is_recommend','title','article_wechat3.updated_at','article_type_wechat3.type_name','form_set','zong_share_num','zong_spot_fabulous_num','keyword_id')->first();
            }else{
                $articleList = ArticleWechat3::join('article_type_wechat3','article_wechat3.article_type','=','article_type_wechat3.id')->where('article_wechat3.id',$id)->where('article_wechat3.status',1)->where('source','LIKE','%'.$source.'%')->select('article_wechat3.id','list_image','is_recommend','title','article_wechat3.updated_at','article_type_wechat3.type_name','form_set','zong_share_num','zong_spot_fabulous_num','keyword_id')->first();
            }
            if(is_null($articleList)){
                $response = Lang::get('error.wechat3_article_not_exist_20000');
                break;
            }
            header("Content-Type: text/html;charset=utf-8");

            if($articleList){
                $form_set = json_decode($articleList->form_set, true);
                if($form_set){
                    $service = [];
                    try{
                        foreach($form_set as $key => $val){
                            if(isset($val['widget_type']) && $val['widget_type'] == 4 && !in_array($val['widget_service'][0]['id'],$service)){
                                $arr = AhpService::where('id',$val['widget_service'][0]['id'])->orderBy('id','desc')->first();
                                if(!is_null($arr)){
                                    $service[$val['widget_service'][0]['id']] = [
                                        'service_id' => $arr->id,
                                        'service_name' => $arr->service_name,
                                        'service_type' => $arr->service_type,
                                        'classify' => $arr->classify,
                                        'thumbnail' => $arr->thumbnail,
                                        'first_picture' => $arr->first_picture,
                                        'ahp_upload_files_url' => Config::get('config.ahp_upload_files_url'),
                                        'num' => isset($arr->num) ? $arr->num : 0,
                                        'start_num' => isset($arr->start_num) ? $arr->start_num : 0,
                                    ];
                                }
                            }
                            $articleList->service = $service;
                        }
                    }catch (Exception $e){
                        Log::error("error", array("system" => "api3.0" ,"event" => "articleShow", 'input' =>Input::all() ,"exception" => $e));
                        //$response = Lang::get("error.user_car_del_error_1009");
                        //break;
                    }

                    $articleList->count = count($articleList->service);
                }
                $keyword_id = explode(',',$articleList->keyword_id);
                $keyword = [];
                foreach($keyword_id as $k){
                    $re = ArticleKeywordWechat3::where('id',$k)->where('status',1)->select('id','keyword_name')->first();
                    if(!is_null($re)){
                        $re = $re->toArray();
                        array_push($keyword,$re);
                    }
                }
                $articleList->keyword = $keyword;
                $articleList->article_first_image_file_url = Config::get('article_share_wechat3.article_first_image_file_url');
                $articleList->article_list_image_file_url = Config::get('article_share_wechat3.article_list_image_file_url');
                $articleList->article_share_image_file_url = Config::get('article_share_wechat3.article_share_image_file_url');
                $articleList->article_wechat3_url = Config::get('article_share_wechat3.article_wechat3_url');

                //判断是否点赞
                $articleList->fabulous = '2'; //未点赞
                if($identification){
                    $spot_fabulous = ArticleStatistics::where('article_id',$id)->where('identification',$identification)->where('type',2)->first();
                    if(!is_null($spot_fabulous)){
                        $articleList->fabulous = '1';  //一点赞
                    }
                }
            }

            $response = array(
                'code' => 0,
                'msg' => 'ok',
                'result' => $articleList
            );
        }while(false);
        Log::info("info", array("system" => "api3.0" ,"event" => "articleShow", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-09-11 13:18:28
     * @Description: C端3.0 文章统计浏览量
     * @param  $token  String  用户token
     * @link: http://www.activity.com/wechat/api3.0/article/articleBrowse?source=1&id=1&model=1&identification=111111&type=1
     */
     public function articleBrowse(){
        $validator = Validator::make(Input::all(),
            array(
                'id' =>'required',
                'source' =>'required',
                'model' =>'required',
                //'identification' =>'required',
                'type' =>'required',
            )
        );
        do{
            if($validator->fails()) {
                $response = Lang::get("ahp_error.args_error_1000");
                $response["result"]=$validator->messages()->all();
                break;
            }

            //获取参数
            $source = Input::get('source');
            $id = Input::get('id');
            $model = Input::get('model');
            $identification = Input::get('identification');
            $type = Input::get('type');

            if($type != 1){
                $response = Lang::get('error.wechat3_article_type_not_exist_20001');
                break;
            }

            //判断文章是否存在
            $article = ArticleWechat3::where('id',$id)->where('status',1)->where('source','!=',0)->first();
            if(!$article){
                $response = Lang::get("error.wechat3_article_not_exist_20000");
                break;
            }

            $url = Config::get('article_share_wechat3.article_wechat3_url').'?id='.$id.'&source='.$source;
            try{
                switch($model){
                    case 1:
                        //微站
                        $rs = array(
                            'zong_browse_num' => DB::raw('zong_browse_num + 1'),
                            'weizhan_browse_num'  => DB::raw('weizhan_browse_num + 1'),
                        );
                        break;
                    case 2:
                        //IOS
                        $rs = array(
                            'zong_browse_num' => DB::raw('zong_browse_num + 1'),
                            'ios_browse_num'  => DB::raw('ios_browse_num + 1'),
                            'app_browse_num'  => DB::raw('app_browse_num + 1'),
                        );
                        break;
                    case 3:
                        //安卓
                        $rs = array(
                            'zong_browse_num' => DB::raw('zong_browse_num + 1'),
                            'android_browse_num'  => DB::raw('android_browse_num + 1'),
                            'app_browse_num'  => DB::raw('app_browse_num + 1'),
                        );
                        break;
                    default:
                        return $response = Lang::get('error.wechat3_article_model_not_exist_20003');
                }
                ArticleWechat3::where("id",$id)->update($rs);

                $article = new ArticleStatistics();
                $article->url = $url;
                $article->article_id = $id;
                $article->source = $source;
                $article->model = $model;
                $article->identification = $identification;
                $article->type = $type;
                $article->created_at = date("Y-m-d H:i:s",time());
                $article->updated_at = date("Y-m-d H:i:s",time());
                $article->save();
                $response = array('code' => 0, 'msg' => 'ok');
            }catch(Exception $e){
                Log::error("error", array("system" => "api3.0" ,"event" => "articleBrowse", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get('ahp_error.wechat3_article_browse_not_exist_20002');
                break;
            }

        }while(false);
        Log::info("info", array("system" => "api3.0" ,"event" => "articleBrowse", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-09-11 13:18:28
     * @Description: C端3.0 文章统计分享
     * @param  $token  String  用户token
     * @link: http://www.activity.com/wechat/api3.0/article/articleShare?source=1&id=23&model=1&identification=111111&type=3
     */
    public function articleShare(){
        $validator = Validator::make(Input::all(),
            array(
                'id' =>'required',
                'source' =>'required',
                'model' =>'required',
                //'identification' =>'required',
                'type' =>'required',
            )
        );
        do{
            if($validator->fails()) {
                $response = Lang::get("ahp_error.args_error_1000");
                $response["result"]=$validator->messages()->all();
                break;
            }

            //获取参数
            $source = Input::get('source');
            $id = Input::get('id');
            $model = Input::get('model');
            $identification = Input::get('identification');
            $type = Input::get('type');

            if($type != 3){
                $response = Lang::get('error.wechat3_article_type_not_exist_20001');
                break;
            }

            //判断文章是否存在
            $article = ArticleWechat3::where('id',$id)->where('status',1)->where('source','!=',0)->first();
            if(!$article){
                $response = Lang::get("error.wechat3_article_not_exist_20000");
                break;
            }

            $url = Config::get('article_share_wechat3.article_wechat3_url').'?id='.$id.'&source='.$source;
            try{
                switch($model){
                    case 1:
                        //微站
                        $rs = array(
                            'zong_share_num' => DB::raw('zong_share_num + 1'),
                            'weizhan_share_num'  => DB::raw('weizhan_share_num + 1'),
                        );
                        break;
                    case 2:
                        //IOS
                        $rs = array(
                            'zong_share_num' => DB::raw('zong_share_num + 1'),
                            'ios_share_num'  => DB::raw('ios_share_num + 1'),
                            'app_share_num'  => DB::raw('app_share_num + 1'),
                        );
                        break;
                    case 3:
                        //安卓
                        $rs = array(
                            'zong_share_num' => DB::raw('zong_share_num + 1'),
                            'android_share_num'  => DB::raw('android_share_num + 1'),
                            'app_share_num'  => DB::raw('app_share_num + 1'),
                        );
                        break;
                    default:
                        return $response = Lang::get('error.wechat3_article_model_not_exist_20003');
                }

                ArticleWechat3::where("id",$id)->update($rs);

                $article = new ArticleStatistics();
                $article->url = $url;
                $article->article_id = $id;
                $article->source = $source;
                $article->model = $model;
                $article->identification = $identification;
                $article->type = $type;
                $article->created_at = date("Y-m-d H:i:s",time());
                $article->updated_at = date("Y-m-d H:i:s",time());
                $article->save();
                $response = array('code' => 0, 'msg' => 'ok');
            }catch(Exception $e){
                Log::error("error", array("system" => "api3.0" ,"event" => "articleShare", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get('ahp_error.wechat3_article_share_not_exist_20004');
                break;
            }

        }while(false);
        Log::info("info", array("system" => "api3.0" ,"event" => "articleShare", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-09-11 13:18:28
     * @Description: C端3.0 文章统计点赞
     * @param  $token  String  用户token
     * @link: http://www.activity.com/wechat/api3.0/article/articleShareSpotFabulous?source=1&id=23&model=1&identification=111111&type=3
     */
    public function articleShareSpotFabulous(){
        $validator = Validator::make(Input::all(),
            array(
                'id' =>'required',
                'source' =>'required',
                'model' =>'required',
                //'identification' =>'required',
                'type' =>'required',
            )
        );
        do{
            if($validator->fails()) {
                $response = Lang::get("ahp_error.args_error_1000");
                $response["result"]=$validator->messages()->all();
                break;
            }

            //获取参数
            $source = Input::get('source');
            $id = Input::get('id');
            $model = Input::get('model');
            $identification = Input::get('identification');
            $type = Input::get('type');

            if($type != 2){
                $response = Lang::get('error.wechat3_article_type_not_exist_20001');
                break;
            }

            //判断文章是否存在
            $article = ArticleWechat3::where('id',$id)->where('status',1)->where('source','!=',0)->first();
            if(!$article){
                $response = Lang::get("error.wechat3_article_not_exist_20000");
                break;
            }

            //判断是否已经点赞
            if($identification){
                $spot_fabulous = ArticleStatistics::where('article_id',$id)->where('source',$source)->where('model',$model)->where('identification',$identification)->where('type',2)->first();
                if(!is_null($spot_fabulous)){
                    $response = Lang::get('error.wechat3_article_spot_fabulous_20006');
                    break;
                }
            }

            $url = Config::get('article_share_wechat3.article_wechat3_url').'?id='.$id.'&source='.$source;
            try{
                switch($model){
                    case 1:
                        //微站
                        $rs = array(
                            'zong_spot_fabulous_num' => DB::raw('zong_spot_fabulous_num + 1'),
                            'weizhan_spot_fabulous_num'  => DB::raw('weizhan_spot_fabulous_num + 1'),
                        );
                        break;
                    case 2:
                        //IOS
                        $rs = array(
                            'zong_spot_fabulous_num' => DB::raw('zong_spot_fabulous_num + 1'),
                            'ios_spot_fabulous_num'  => DB::raw('ios_spot_fabulous_num + 1'),
                            'app_spot_fabulous_num'  => DB::raw('app_spot_fabulous_num + 1'),
                        );
                        break;
                    case 3:
                        //安卓
                        $rs = array(
                            'zong_spot_fabulous_num' => DB::raw('zong_spot_fabulous_num + 1'),
                            'android_spot_fabulous_num'  => DB::raw('android_spot_fabulous_num + 1'),
                            'app_spot_fabulous_num'  => DB::raw('app_spot_fabulous_num + 1'),
                        );
                        break;
                    default:
                        return $response = Lang::get('error.wechat3_article_model_not_exist_20003');
                }

                ArticleWechat3::where("id",$id)->update($rs);

                $article = new ArticleStatistics();
                $article->url = $url;
                $article->article_id = $id;
                $article->source = $source;
                $article->model = $model;
                $article->identification = $identification;
                $article->type = $type;
                $article->created_at = date("Y-m-d H:i:s",time());
                $article->updated_at = date("Y-m-d H:i:s",time());
                $article->save();
                $response = array('code' => 0, 'msg' => 'ok');
            }catch(Exception $e){
                Log::error("error", array("system" => "api3.0" ,"event" => "articleShareSpotFabulous", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get('error.wechat3_article_spot_fabulous_not_exist_20005');
                break;
            }

        }while(false);
        Log::info("info", array("system" => "api3.0" ,"event" => "articleShareSpotFabulous", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-09-11 09:18:28
     * @Description: C端3.0 文章分类
     * @param  $token  String  用户token
     * @link: http://www.activity.com/wechat/api3.0/article/articleType
     */
    public function articleType(){
        $validator = Validator::make(Input::all(),
            array(
               // 'source' =>'required',
            )
        );
        do{
            if($validator->fails()) {
                $response = Lang::get("ahp_error.args_error_1000");
                $response["result"]=$validator->messages()->all();
                break;
            }

            //获取参数
            $source = Input::get('source');

            //查询文章分类
            $articleType = ArticleTypeWechat3::where('status',1)->select('id','type_name')->get()->toArray();

            $response = array(
                'code' => 0,
                'msg' => 'ok',
                'result' => [
                    'articleType' => $articleType,
                ]
            );
        }while(false);
        Log::info("info", array("system" => "api3.0" ,"event" => "articleType", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-09-11 09:18:28
     * @Description: C端3.0 文章分享
     * @param  $token  String  用户token
     * @link: http://www.activity.com/wechat/api3.0/article/share?id=23
     */
    public function share(){
        $validator = Validator::make(Input::all(),
            array(
                "id"=>'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }

            $shareSet = ['title'=>Config::get('wechat3.default_share_title'),
                'url'=> Config::get('wechat3.default_share_url'),
                'image'=>Config::get('wechat3.default_share_img'),
                'desc'=>Config::get('wechat3.default_share_desc')];//    分享默认值
            $id = Input::get('id');
            $type = Input::get('type',1);

            $wechat_share_data = ArticleShareWechat3::where('article_id',$id)->first();
            if(!is_null($wechat_share_data)){
                $shareSet['title'] = $wechat_share_data->title;
                $shareSet['url'] = Config::get('wechat3.redirect_url').$wechat_share_data->share_url.'?id='.$id.'&type='.$type;
                $shareSet['desc'] = $wechat_share_data->desc;
                $shareSet['image'] = Config::get('article_share_wechat3.article_share_image_file_url').$wechat_share_data->img_url;
            }

            $response = array('code' => 0, 'msg' => 'ok','shareSet' => $shareSet);
        }while(false);
        Log::info("info", array("system" => "api3.0" ,"event" => "share", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-10-25 14:00:28
     * @Description: C端3.0 文章评论
     * @param  $token  String  用户token
     * @param  $content  String  评论内容
     * @link: http://www.activity.com/wechat/api3.0/article/articleAddComment?token=6cf73dd28707689a2b4fc3f497c1758b&content=111111111&article_id=14&id=1
     */
    public function articleAddComment(){
        $validator = Validator::make(Input::all(),
            array(
                "token"=>'required',
                "content"=>'required',
                "article_id"=>'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $users=$this->verificationUserToken(Input::get("token"),$response);
            if(is_null($users)){
                break;
            }

            //获取参数
            $content = $str = Input::get('content');
            $id = Input::get('id'); //父级id
            $article_id = Input::get('article_id'); //文章id

            //校验文章是否存在
            $article_frist = ArticleWechat3::where('id',$article_id)->where('status',1)->where('source','!=',0)->first();
            if(is_null($article_frist)){
                $response = Lang::get('error.wechat3_article_not_exist_20000');
                break;
            }

            if(!empty($id)){
                $article = ArticleCommentWechat3::find($id);
                if(is_null($article)){
                    $response = Lang::get('error.wechat3_article_reviews_floor_not_exist_error_20010');
                    break;
                }
            }

            $key = 'demand_sensitive_word';
            //$temp = Redis::del($key);die; //删除
            $temp = Redis::get($key);

            if(!$temp){
                $temp = DemandSensitiveWord::all()->toArray();
                $temp = base64_encode(json_encode($temp));
                Redis::setex( $key , 60*24*365, $temp );
            }

            $demand_sensitive_word = json_decode(base64_decode($temp),true);
            $badword = array();
            if(!is_null($demand_sensitive_word)){
                $badword = array_column($demand_sensitive_word,'badword');
            }
            if(!empty($badword)){
                $badword1 =array_combine($badword,array_fill(0,count($badword),'*'));
                $str = strtr($content,$badword1);
            }

            $comment = ArticleCommentWechat3::commentAdd($id,$content,$str,$users,$article_id,1,1);
            $article = ArticleCommentWechat3::with('parent')->find($comment->id);
            if(!is_null($article)){
                if($article->Irregularities == 1){
                    $article->content_show = Config::get('article_share_wechat3.article_Irregularities_message');
                }
            }
            if(!empty($article->parent['Irregularities'])){
                if($article->parent['Irregularities'] == 1){
                    $article->parent['content_show'] = Config::get('article_share_wechat3.article_Irregularities_message');
                }
            }

            $response = array('code' => 0, 'msg' => 'ok','comment' => $article);
        }while(false);
        Log::info("info", array("system" => "api3.0" ,"event" => "articleComment", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-10-25 15:35:28
     * @Description: C端3.0 评论列表
     * @param  $token  String  用户token
     * @link: http://www.activity.com/wechat/api3.0/article/articleCommentList?token=d93a2b6b014512bbaa9d6901c4b51923&page=1&article_id=14&page_size=10
     */
    public function articleCommentList(){
        $validator = Validator::make(Input::all(),
            array(
                //"token"=>'required',
                "article_id"=>'required',
                "page"=>'required',
                "page_size"=>'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
//            $users=$this->verificationUserToken(Input::get("token"),$response);
//            if(is_null($users)){
//                break;
//            }

            //获取参数
            $page = Input::get('page');
            $page_size = Input::get('page_size');
            $article_id = Input::get('article_id');  //文章id

            //校验文章是否存在
            $article_frist = ArticleWechat3::where('id',$article_id)->where('status',1)->where('source','!=',0)->first();
            if(is_null($article_frist)){
                $response = Lang::get('error.wechat3_article_not_exist_20000');
                break;
            }
            //查询评论
//            $article_sort = ArticleCommentWechat3::sortEffectiveDiGui($article_id,1,1);
//            $article = ArticleCommentWechat3::sortEffectiveDiGui($article_id,1,2);
//            $results = array_merge($article_sort,$article);
//            $count = sizeof($results);
//            $result = array_slice($results, ($page-1)*$page_size, $page_size);
//            if(!empty($result)){
//                foreach($result as $key => $val){
//                    if($val['Irregularities'] == 1){
//                        $result[$key]['content_show'] = Config::get('article_share_wechat3.article_Irregularities_message');
//                    }
//                    if($val['parent']['Irregularities'] == 1){
//                        $result[$key]['parent']['content_show'] = Config::get('article_share_wechat3.article_Irregularities_message');
//                    }
//                }
//            }

            //新版
            $article = ArticleCommentWechat3::sortEffectiveDiGui($article_id);
            $tmp_sort = array();
            $tmp = array();
            if(!empty($article)){
                foreach($article as $key => $val){
                    if($val['sort'] == 1){
                        array_push($tmp_sort,$val);
                    }else{
                        array_push($tmp,$val);
                    }
                }
            }
            if(!empty($tmp_sort)){
                array_multisort(array_column($tmp_sort,'created_at'),SORT_DESC,$tmp_sort);
            }
            if(!empty($tmp)){
                array_multisort(array_column($tmp,'created_at'),SORT_DESC,$tmp);
            }
            $results = array_merge($tmp_sort,$tmp);
            $count = sizeof($results);
            $result = array_slice($results, ($page-1)*$page_size, $page_size);
            if(!empty($result)){
                foreach($result as $key => $val){
                    if($val['Irregularities'] == 1){
                        $result[$key]['content_show'] = Config::get('article_share_wechat3.article_Irregularities_message');
                    }
                    if($val['parent']['Irregularities'] == 1){
                        $result[$key]['parent']['content_show'] = Config::get('article_share_wechat3.article_Irregularities_message');
                    }
                }
            }

            $response = array('code' => 0, 'msg' => 'ok','result' => array('count' => $count, 'lists' => $result));
        }while(false);
        Log::info("info", array("system" => "api3.0" ,"event" => "articleCommentList", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-10-31 15:35:28
     * @Description: C端3.0 精选关键词列表
     * @param  $token  String  用户token
     * @link: http://www.activity.com/wechat/api3.0/article/articleKeyWordtList
     */
    public function articleKeyWordtList(){
        $validator = Validator::make(Input::all(),
            array(

            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }

            //精选关键词
            $keyword = ArticleKeywordWechat3::where('status',1)->where('is_recommend',1)->select('id','keyword_name')->get();

            $response = array('code' => 0, 'msg' => 'ok','result' => array('lists' => $keyword));
        }while(false);
        Log::info("info", array("system" => "api3.0" ,"event" => "articleKeyWordtList", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

}